<?php
/**
 * created by zzy
 * date: 2017/10/24 9:28
 */

namespace App\Http\Admin\Process\Controllers;

use Framework\BaseClass\Http\Admin\Controller;
use App\Eloquent\Ygt\OrderField;
use App\Eloquent\Ygt\OrderFieldCompany;
use App\Eloquent\Ygt\OrderFieldCompanySelect;

class OrderFieldController extends Controller
{

    public function set()
    {
        $adminUser          = request()->user('admin')->toArray();
        $companyId          = $adminUser['company_id'];

        $fieldId           = request('field_id',0);
        if (request()->isMethod('post')) {

            $title_type      = request('title_type','');
            $params          = $this->requestJson();
            $id              = isset($params['id']) ? $params['id'] : 0;
            $pid             = isset($params['pid']) ? $params['pid'] : 0;
            $title           = $params['title'];
            $isDefault       = isset($params['is_default']) ? $params['is_default'] : 0;
            if( $title ){
                if($isDefault == 1)
                {
                    $dafaultData    = ['is_default'=>0];
                    OrderFieldCompanySelect::where('company_id',$companyId)
                        ->where('field_id',$fieldId)->update($dafaultData);
                }
                $data           = [
                    'company_id'=>$companyId,'field_id'=>$fieldId,'pid'=>$pid,
                    'title'=>$title,'title_type'=>$title_type,'is_default'=>$isDefault
                ];
                if( $id ){
                    $where      = ['id'=>$id];
                    $result     = OrderFieldCompanySelect::updateOneData( $where, $data );
                } else {
                    $result     = OrderFieldCompanySelect::insertOneData( $data );
                }
                if( $result ){
                    return $this->ajaxSuccess('成功');
                } else {
                    return $this->ajaxFail('失败');
                }
            } else {
                return $this->ajaxFail('选项标题不能为空');
            }

        } else {

            $where                  = ['company_id'=>$companyId, 'field_id'=>$fieldId];
            $info                   = OrderFieldCompany::getInfo( $where );
            $fieldType              = $info->field_type;

            $where                  = ['company_id'=>$companyId,'field_id'=>$fieldId];
            $collection             = OrderFieldCompanySelect::getList($where);
            $lists                  = $collection->toArray();
            $selectList             = $selectList2 = [];
            $viewBlade              = 'set';
            switch ($fieldType)
            {
                case 14:
                    $viewBlade      .= '-14';
                    foreach( $lists as $key=>$val )
                    {
                        if( $val['title_type'] == '14s2' )
                        {
                            $selectList2[]     = $val;
                        } else {
                            $selectList[]      = $val;
                        }
                    }
                    break;
                case 11:
                    $viewBlade          .= '-11';
                    $selectList         = $lists;
                    break;
                default:
                    $selectList         = $lists;
            }
            $selectListJson         = json_encode($selectList);
            $selectListJson         = str_replace('"', "'",$selectListJson);
            $selectListJson2        = json_encode($selectList2);
            $selectListJson2        = str_replace('"', "'",$selectListJson2);

            return $this->view($viewBlade, compact( 'info', 'selectListJson','selectListJson2' ) );
        }
    }
    public function delete(){
        $id                 = request('id',0);
        if( $id ){
            $id_arr         = explode(',',$id);
            if( in_array( 0, $id_arr ) ){
                return $this->ajaxFail('失败0');
            }
            $where          = ['id'=>['in',$id_arr]];
            $del_result     = OrderFieldCompanySelect::del($where);
            if( $del_result !== false ){
                //删除子列表
                $where          = ['pid'=>['in',$id_arr]];
                $del_result     = OrderFieldCompanySelect::del($where);
                if( $del_result !== false ){
                    return $this->ajaxSuccess('成功');
                } else {
                    return $this->ajaxFail('失败2');
                }
            } else {
                return $this->ajaxFail('失败2');
            }
        } else {
            return $this->ajaxFail('失败3');
        }
    }
}
